Repository navigation
Fix tests which changed CWD without resetting - #54599
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a source of test flakiness in dotnet.Tests by ensuring RunParserTests no longer leaks process-wide current working directory (CWD) changes into later tests, and by removing an unnecessary SetCurrentDirectory call in the -- argument parsing test.
Changes:
- Add per-test CWD restoration to
RunParserTestsviaIDisposable. - Remove redundant
Directory.SetCurrentDirectory(...)usage where the project path is already absolute. - Keep the existing path-separator tests’ CWD behavior, but ensure it is reset after each test instance.
Youssef1313
approved these changes
Jun 5, 2026
Member
There was a problem hiding this comment.
LGTM
You can also consider using a custom xunit test framework implementation to assert this invariant for every test method in the whole assembly.
public class MyTestFramework : XunitTestFramework
{
private sealed class MyDiscoverer : ITestFrameworkDiscoverer
{
private readonly ITestFrameworkDiscoverer _wrapped;
public MyDiscoverer(ITestFrameworkDiscoverer wrapped)
=> _wrapped = wrapped;
public ITestAssembly TestAssembly => _wrapped.TestAssembly;
public ValueTask Find(Func<ITestCase, ValueTask<bool>> callback, ITestFrameworkDiscoveryOptions discoveryOptions, Type[] types = null, CancellationToken? cancellationToken = null)
=> _wrapped.Find(testCase => callback(new MyTestCase((IXunitTestCase)testCase)), discoveryOptions, types, cancellationToken);
}
private sealed class MyTestCase : IXunitTestCase, ISelfExecutingXunitTestCase
{
private readonly IXunitTestCase _wrapped;
public MyTestCase(IXunitTestCase wrapped)
=> _wrapped = wrapped;
public Type[] SkipExceptions => _wrapped.SkipExceptions;
public string SkipReason => _wrapped.SkipReason;
public Type SkipType => _wrapped.SkipType;
public string SkipUnless => _wrapped.SkipUnless;
public string SkipWhen => _wrapped.SkipWhen;
public IXunitTestClass TestClass => _wrapped.TestClass;
public int TestClassMetadataToken => _wrapped.TestClassMetadataToken;
public string TestClassName => _wrapped.TestClassName;
public string TestClassSimpleName => _wrapped.TestClassSimpleName;
public IXunitTestCollection TestCollection => _wrapped.TestCollection;
public IXunitTestMethod TestMethod => _wrapped.TestMethod;
public int TestMethodMetadataToken => _wrapped.TestMethodMetadataToken;
public string TestMethodName => _wrapped.TestMethodName;
public string[] TestMethodParameterTypesVSTest => _wrapped.TestMethodParameterTypesVSTest;
public string TestMethodReturnTypeVSTest => _wrapped.TestMethodReturnTypeVSTest;
public int Timeout => _wrapped.Timeout;
public bool Explicit => _wrapped.Explicit;
public string SourceFilePath => _wrapped.SourceFilePath;
public int? SourceLineNumber => _wrapped.SourceLineNumber;
public string TestCaseDisplayName => _wrapped.TestCaseDisplayName;
public string TestClassNamespace => _wrapped.TestClassNamespace;
public int? TestMethodArity => _wrapped.TestMethodArity;
public IReadOnlyDictionary<string, IReadOnlyCollection<string>> Traits => _wrapped.Traits;
public string UniqueID => _wrapped.UniqueID;
ITestClass ITestCase.TestClass => TestClass;
ITestCollection ITestCase.TestCollection => TestCollection;
ITestMethod ITestCase.TestMethod => TestMethod;
int? ITestCaseMetadata.TestClassMetadataToken => TestClassMetadataToken;
int? ITestCaseMetadata.TestMethodMetadataToken => TestMethodMetadataToken;
public ValueTask<IReadOnlyCollection<IXunitTest>> CreateTests() => _wrapped.CreateTests();
public void PostInvoke() => _wrapped.PostInvoke();
public void PreInvoke() => _wrapped.PreInvoke();
public ValueTask<RunSummary> Run(ExplicitOption explicitOption, IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource)
{
var previousDirectory = Directory.GetCurrentDirectory();
try
{
return await XunitRunnerHelper.RunXunitTestCase(
this,
messageBus,
cancellationTokenSource,
aggregator,
explicitOption,
constructorArguments);
}
finally
{
// If any test modified current directory without restoring it, this assert will fail.
Assert.Equal(previousDirectory, Directory.GetCurrentDirectory());
}
}
}
protected override ITestFrameworkDiscoverer CreateDiscoverer(Assembly assembly) => new MyDiscoverer(base.CreateDiscoverer(assembly));
protected override ITestFrameworkExecutor CreateExecutor(Assembly assembly) => base.CreateExecutor(assembly);
}MSBuild does that here
This was referenced Jun 5, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix flaky
AddReferenceHasDefaultArgumentSetToCurrentDirectorytestRunParserTestscalledDirectory.SetCurrentDirectorywithout restoring it, leaking the process-wide current directory into later tests in the assembly. When it ran beforeAddReferenceHasDefaultArgumentSetToCurrentDirectory, that test'sDirectory.GetCurrentDirectory()no longer matched the argument's startup-cached default, causing a spurious failure.Fix: restore the current directory after each test via
IDisposable, and drop an unnecessarySetCurrentDirectoryfrom the double-dash test (its project path is already absolute).Seen in PR run for #54591